package com.example.weatherpiano;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager2.widget.ViewPager2;


import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.LinearLayout;


import com.example.weatherpiano.Adapter.MyFragmentPagerAdapter;
import com.example.weatherpiano.Adapter.MyRecycleViewAdapter;
import com.example.weatherpiano.DAO.City;
import com.example.weatherpiano.DAO.CurrentWeather;
import com.example.weatherpiano.DAO.RecWeather;
import com.example.weatherpiano.Fragment.city_weather;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ViewPager2 viewPager;
    TabLayout tabLayout;
    RecyclerView recyclerView;
    MyRecycleViewAdapter recycleViewAdapter;
    List<City> cits;
    List<RecWeather> recWeatherList0;
    List<RecWeather> recWeatherList1;
    List<RecWeather> recWeatherList2;
    List<CurrentWeather> weatherList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        初始化测试数据
        initData();
//        初始化页面
        initPage();
//        滑动 viewpager 时切换 recycle view内容
        changeCity();
    }

//    添加菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

//   菜单栏操作
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case R.id.about :
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("关于 天琴(WeatherPiano)");
                dialog.setMessage("作者：刘志远 \n 🥰");
                dialog.setPositiveButton("OK", null);
                dialog.setNegativeButton("OKKKK", null);
                dialog.show();
                break;
            case R.id.select_city :
                Intent intent = new Intent(MainActivity.this, CitsActivity.class);
                String [] cityList = {cits.get(0).getCity_name(),cits.get(1).getCity_name(),cits.get(2).getCity_name() };
                intent.putExtra("cityList", cityList);
                startActivityForResult(intent, 1);

        }
        return super.onOptionsItemSelected(item);

    }

//    未实现
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initPage() {

//        获取界面组件
        viewPager = findViewById(R.id.now_weather);
        tabLayout = findViewById(R.id.tab_layout);
        recyclerView = findViewById(R.id.recent_weather);

//        创建已选择的城市的当前天气界面
        ArrayList<Fragment> fragmentList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            fragmentList.add(city_weather.newInstance(cits.get(i)));

        }
        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        viewPager.setAdapter(fragmentPagerAdapter);


//        关联tab栏和viewpager
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> tab.setText(cits.get(position).getCity_name())).attach();

//        设置天气预报列表
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, LinearLayout.VERTICAL);
        recycleViewAdapter = new MyRecycleViewAdapter(recWeatherList0, this);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        recyclerView.setAdapter(recycleViewAdapter);
        recyclerView.setNestedScrollingEnabled(false);

    }


    private void initData() {
        weatherList = new ArrayList<>();
        recWeatherList0 = new ArrayList<>();
        recWeatherList1 = new ArrayList<>();
        recWeatherList2 = new ArrayList<>();
        cits = new ArrayList<>();

        String date = "12-21";

        weatherList.add(new CurrentWeather(0, "1-2", 0, "晴朗", 10));
        weatherList.add(new CurrentWeather(1, "1-3", 1, "多云", 5));
        weatherList.add(new CurrentWeather(2, "1-4", 2, "小雨", 22));

        recWeatherList0.add(new RecWeather(0, 0, date, "晴朗", 10, 1));
        recWeatherList0.add(new RecWeather(1, 0, date, "晴朗", 11, 2));
        recWeatherList0.add(new RecWeather(2, 0, date, "晴朗", 12, 3));

        recWeatherList1.add(new RecWeather(0, 1, date, "晴朗", 2, 1));
        recWeatherList1.add(new RecWeather(1, 1, date, "多云", 1, 0));
        recWeatherList1.add(new RecWeather(2, 1, date, "多云", 0, -1));

        recWeatherList2.add(new RecWeather(0, 2, date, "晴朗", 24, 18));
        recWeatherList2.add(new RecWeather(1, 2, date, "小雨", 23, 11));
        recWeatherList2.add(new RecWeather(2, 2, date, "多云", 22, 13));

        cits.add(new City(0, recWeatherList0, weatherList.get(0), "信阳"));
        cits.add(new City(1, recWeatherList1, weatherList.get(1), "黑龙江"));
        cits.add(new City(2, recWeatherList2, weatherList.get(2), "海南"));
    }

    private void changeCity() {
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

            @Override
            public void onPageSelected(int position) {
                recycleViewAdapter.changeItems(cits.get(position).getRecWeatherList());
            }
        });
    }

}